package com.baobao.druid.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.servlet.Filter;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author baobao
 * @create 2020-09-04 15:11
 * @description
 */
@Configuration
public class DruidConfig {
    /*// 注入数据源ds1，并且作为首选的数据源
    @Primary
    @Bean
    @ConfigurationProperties("spring.datasource.druid.ds1")
    public DataSource dataSourceOne(){
        return DruidDataSourceBuilder.create().build();
    }

    // 注入数据源ds2
    @Bean
    @ConfigurationProperties("spring.datasource.druid.ds2")
    public DataSource dataSourceTwo(){
        return DruidDataSourceBuilder.create().build();
    }*/

    // 配置Druid的监控页面
    // 1、配置一个管理后台的Servlet
    /*@Bean
    public ServletRegistrationBean statViewServlet(){
        // 请求映射到/druid/*
        ServletRegistrationBean<StatViewServlet> servletRegistrationBean
                = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        // 给Servlet添加初始化参数
        Map<String, String> initParams = new HashMap<>();
        // 监控页面登陆的用户名
        initParams.put("loginUsername", "baobao");
        // 监控页面登陆的密码
        initParams.put("loginPassword", "123");
        // 允许登陆的主机，默认所有主机都允许登陆
        initParams.put("allow", "");
        // 不允许登陆的主机
        initParams.put("deny", "192.169.0.131");
        servletRegistrationBean.setInitParameters(initParams);
        return servletRegistrationBean;
    }

    // 2、配置一个web监控的filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
        filterFilterRegistrationBean.setFilter(new WebStatFilter());
        // 给拦截器添加url映射，拦截所有请求
        filterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        // 给拦截器添加初始化参数
        Map<String, String> initParams = new HashMap<>();
        // 设置不拦截的url
        initParams.put("exclusions","*.js,*.css,/druid/*");
        filterFilterRegistrationBean.setInitParameters(initParams);
        return filterFilterRegistrationBean;
    }*/
}
